﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;

public partial class admin_DistributorDetail : SitePage
{
  

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();          
        }
    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        InitData();
    }
    #endregion

    #region Methods
    private void LoadStateByCountry(string stateCode)
    {
        ddlState.Items.Clear();
        if (ddCountry.SelectedValue == clsConfigData.DEFAULT_CUSTOMER_US_COUNTRY_CODE)
        {
            busState objBus = new busState();
            StateCollection lstState = objBus.GetByCountry(ddCountry.SelectedValue);
            ddlState.DataSource = lstState;
            ddlState.DataTextField = busState.STATENAME;
            ddlState.DataValueField = busState.STATECODE;
            ddlState.DataBind();
        }
        ddlState.Items.Insert(0, new ListItem("- - -", ""));
        ddlState.SelectedValue = stateCode;
    }
    private void LoadCountryData(string countryCode,string state)
    {
        busCountry objBus = new busCountry();       
        ddCountry.Items.Clear();
        if (countryCode == clsConfigData.DEFAULT_CUSTOMER_US_COUNTRY_CODE)//customer in US
        {
            ddCountry.Items.Add(new ListItem(clsConfigData.DEFAULT_CUSTOMER_US_COUNTRY_CODE, clsConfigData.DEFAULT_CUSTOMER_US_COUNTRY_CODE));
            //Load State data
            LoadStateByCountry(state);
        }
        else
        {
            CountryCollection lstCountry = objBus.GetAll();
            foreach (Country objCountry in lstCountry)
            {
                if (objCountry.CountryCode != clsConfigData.DEFAULT_CUSTOMER_US_COUNTRY_CODE)//international  customer
                {
                    ddCountry.Items.Add(new ListItem(objCountry.CountryName, objCountry.CountryCode));
                }
            }
        }
        ddCountry.Items.Insert(0, new ListItem("- - -", ""));
        ddCountry.SelectedValue = countryCode;
    }
    private void LoadData()
    {
        int id = clsQueryString.GetInt("id");       
        busDistributor objBus = new busDistributor();
        Distributor objDistributor = objBus.GetByID(id);
        if (objDistributor != null)
        {
            txtAddress1.Text = objDistributor.Address1;
            txtAddress2.Text = objDistributor.Address2;
            txtCity.Text = objDistributor.City;
            txtCompanyName.Text = objDistributor.CompanyName;
            txtContactName.Text = objDistributor.ContactName;
            txtEmail.Text = objDistributor.Email;
            txtFax.Text = objDistributor.Fax;
            txtComment.Text = objDistributor.Note;
            txtPhone.Text = objDistributor.Phone;
            txtProvince.Text = objDistributor.Province;
            txtPostalCode.Text = objDistributor.ZipCode;
            if (objDistributor.IsActive)
                chkIsActive.Checked = true;
            else chkIsActive.Checked = false;
            LoadCountryData(objDistributor.CountryCode,objDistributor.StateCode);
            SetDefaultData();          
        }
    }
    private void InitData()
    {
        string validateGroup = "customer";
        vsumDistributor.ValidationGroup = reqCountry.ValidationGroup = btnSave.ValidationGroup = reqCompanyName.ValidationGroup = validateGroup;
        reqAddress1.ValidationGroup = reqCity.ValidationGroup = reqContactName.ValidationGroup = validateGroup;
        reqEmail.ValidationGroup = reqPhone.ValidationGroup = reqPostalCode.ValidationGroup = reqProvince.ValidationGroup = reqState.ValidationGroup = validateGroup;
        cusEmail.ValidationGroup = cusPhone.ValidationGroup = cusPostalCode.ValidationGroup = validateGroup;
        //Init message
        reqCountry.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Country");
        reqCompanyName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Company name");
        reqContactName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Contact name");
        reqAddress1.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Address1");
        reqCity.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "City");
        reqState.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "State");
        reqPostalCode.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Zip/ Postal Code");
        reqProvince.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Province/Region");
        reqPhone.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Phone");
        reqEmail.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Email");       
        cusEmail.ErrorMessage = string.Format(Resources.Resource.MSG_VALID, "Email");       
        cusPhone.ErrorMessage = string.Format(Resources.Resource.MSG_VALID, "Phone");
        cusPostalCode.ErrorMessage = string.Format(Resources.Resource.MSG_VALID, "Zip/ Postal Code");
    }

    private void UpdateDistributor(Distributor objDis)
    {
        busDistributor objBus = new busDistributor();
        objDis.DistributorID = clsQueryString.GetInt("id");
        if (objBus.Update(objDis))
            Response.Redirect("DistributorList.aspx?" + this.GetPageQuery);
        else lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }
    private void SetDefaultData()
    {
        string countryCode = ddCountry.SelectedValue;
        if (countryCode == clsConfigData.DEFAULT_CUSTOMER_US_COUNTRY_CODE)//customer in US
        {          
            trCountry.Visible = false;
            reqCountry.Visible = false;
            trProvince.Visible = false;
            reqProvince.Visible = false;
            reqPostalCode.Visible = true;
            cusPostalCode.Visible = true;
            lblZipCode.Text += "(<span class='commentRequireInputText'>*</span>)";
        }
        else//international customer
        {         
            trState.Visible = false;
            reqState.Visible = false;
            reqPostalCode.Visible = false;
            cusPostalCode.Visible = false;

        }
        lblZipCode.Text += ":";
    }
    #endregion

    #region Events

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //get datavalue
            Distributor objDistributor = new Distributor();
            objDistributor.Address1 = txtAddress1.Text.Trim();
            objDistributor.Address2 = txtAddress2.Text.Trim();
            objDistributor.City = txtCity.Text.Trim();
            objDistributor.CompanyName = txtCompanyName.Text.Trim();
            objDistributor.ContactName = txtContactName.Text.Trim();
            objDistributor.CountryCode = ddCountry.SelectedValue;
            objDistributor.Email = txtEmail.Text.Trim();
            objDistributor.Fax = txtFax.Text.Trim();
            if(chkIsActive.Checked)
                 objDistributor.IsActive = true;
            else objDistributor.IsActive = false;
            objDistributor.Note = txtComment.Text.Trim();
            objDistributor.Phone = txtPhone.Text.Trim();           
            objDistributor.ZipCode = txtPostalCode.Text.Trim();
            if (ddCountry.SelectedValue == clsConfigData.DEFAULT_CUSTOMER_US_COUNTRY_CODE)//customer in US
            {
                objDistributor.StateCode = ddlState.SelectedValue;
                objDistributor.Province = string.Empty;
            }
            else
            {
                objDistributor.StateCode = string.Empty;
                objDistributor.Province = txtProvince.Text;
            }
            UpdateDistributor(objDistributor);
        }
        else
        {
            vsumDistributor.ShowMessageBox = false;
            vsumDistributor.ShowSummary = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("DistributorList.aspx?" + this.GetPageQuery);
    }
    #endregion
    #region Validate Data
    protected void CheckSZipCode(object source, ServerValidateEventArgs args)
    {
        args.IsValid = clsValidate.CheckPostalCodeValid(args.Value);
    }
    protected void CheckSPhone(object source, ServerValidateEventArgs args)
    {
        args.IsValid = clsValidate.CheckPhoneValid(args.Value);
    }
    protected void CheckSEmail(object source, ServerValidateEventArgs args)
    {
        args.IsValid = clsValidate.CheckEmailValid(args.Value);
    }
    #endregion
}